package gw.common.tool;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HtmlOperation {
	public static ArrayList<String> urlDetector(String htmlDoc) throws MalformedURLException
	{
		final String patternString = "<[a|A]\\s+href=([^>]*\\s*>)";
		Pattern pattern = Pattern.compile(patternString,Pattern.CASE_INSENSITIVE);
		ArrayList<String> allUrls = new ArrayList<String>();
		Matcher matcher = pattern.matcher(htmlDoc);
		 String tempURL;
		    //初次匹配到的url是形如：<a href="http://bbs.life.xxx.com.cn/" target="_blank">
		    //为此，需要进行下一步的处理，把真正的url抽取出来，
			//可以对于前两个"之间的部分进行记录得到url
		    while(matcher.find()){
		        tempURL = matcher.group();            
				tempURL = tempURL.substring(tempURL.indexOf("\"")+1);        
				if(!tempURL.contains("\""))
				    continue;
				tempURL = tempURL.substring(0, tempURL.indexOf("\""));
				allUrls.add(tempURL);
		    }
		    return allUrls;    
	}
}
